//
//
//
//
//
//
// DispatchMsg.cpp
//

#include "stdafx.h"
#include "DispatchMsg.h"
#include "WinUtil.h"
#include "Resource.h"
#include "ScreenVer.h"


// Dispatch hot key msg
// GIC_HOTKEY_NOTIFY_ACTION_CODE	set focus of the screen ctrl+alt+a
// GIC_HOTKEY_NOTIFY_QUIT_CODE		quit the process
//
BOOL DispatchHotKeyMsg(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(wParam)
	{
	case GIC_HOTKEY_NOTIFY_ACTION_CODE:
		{		

			if(copyDC.State() != DCUtil::RANG_INITIAL)
				break;
			// Desktop rect rang
			RECT rect;

			HWND hDesktopWindow = GetDesktopWindow();
			GetClientRect(hDesktopWindow,&rect);
			HDC hDesktopDC = GetDC(hDesktopWindow);

			if(!_hBitMap)
				::DeleteObject(_hBitMap);
			_hBitMap = CopyDCToBitmap(hDesktopDC, &rect);
			::ReleaseDC(hDesktopWindow, hDesktopDC);


			// Then copy desktop DC to the current window
			::ShowWindow(hWnd, SW_MAXIMIZE);
		}
		break;

	case GIC_HOTKEY_NOTIFY_QUIT_CODE:
		{
			::PostQuitMessage(0);
		}
		break;

	default:
		break;
	}
	return TRUE;
}


// Process normal key message here
// WM_KEYDOWN
// WM_KEYUP
BOOL DispatchKeyMsg(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_KEYUP:
		{
			// If we already select rang from that bitmap, clr the rang
			// else, minisize the window.
			if(wParam == VK_ESCAPE && copyDC.State() > DCUtil::RANG_INITIAL)
			{
				copyDC.Clr();
				RePaintBMP(hWnd, _hBitMap, copyDC);	
			}
			else if(wParam == VK_ESCAPE)
			{				
				ShowWindow(hWnd, SW_HIDE);
			}
			break;
		}
	default:
		return FALSE;
		break;
	}

	return TRUE;
}


//
// DispatchContextMenuMsg
// IDM_EXIT		PostQuitMessage
// IDM_VERSION	Show version message box
//
BOOL DispatchContextMenuMsg(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (LOWORD(wParam)) 
	{
	case IDM_EXIT:
		::PostQuitMessage(0);
		break;

	case IDM_MENU_VERSION:
	case IDM_VERSION:
		{
			::DialogBox(hInst, MAKEINTRESOURCE(IDD_VERSIONDLG), hWnd, ScreenVerDefFun);
			break;
		}

	case IDM_MENU_SAVE_AS:
		{	
			OPENFILENAME Ofn;
			TCHAR szFile[100];
			TCHAR szFileTitle[100];

			ZeroMemory(szFile, sizeof(szFile));
			ZeroMemory(szFile, sizeof(szFileTitle));
			Ofn.lStructSize = sizeof(OPENFILENAME); 
			Ofn.hwndOwner = hWnd; 			
			Ofn.lpstrFilter = _TEXT("All(*.*)\0*.*\0bmp files(*.bmp)\0*.bmp\0"); 
			Ofn.nFilterIndex = 1;
			Ofn.lpstrFile= szFile; 			
			Ofn.nMaxFile = sizeof(szFile)/ sizeof(*szFile); 
			Ofn.lpstrFileTitle = szFileTitle; 
			Ofn.nMaxFileTitle = sizeof(szFileTitle); 
			Ofn.lpstrInitialDir = (LPCWSTR)NULL; 
			Ofn.Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT; 
			Ofn.lpstrTitle = _TEXT("Save as"); 

			if(::GetSaveFileName (&Ofn))
			{
				RECT rect;
				copyDC.ToRect(rect);
				copyDC.Clr();

				HDC _windowDC = ::GetDC(hWnd);
				HBITMAP rangBitMap = CopyDCToBitmap(_windowDC, &rect);							
				SaveBmp(Ofn.lpstrFile, rangBitMap);
				::ReleaseDC(hWnd, _windowDC);

				ShowWindow(hWnd, SW_HIDE);							
			}
			break;
		}		

	case IDC_CONTENTSCREEN_COPY:
		{
			RECT rect;
			copyDC.ToRect(rect);
			copyDC.Clr();

			HDC _windowDC = ::GetDC(hWnd);
			HBITMAP rangBitMap = CopyDCToBitmap(_windowDC, &rect);
			::ReleaseDC(hWnd, _windowDC);
			SaveBmpToClipboard(hWnd, rangBitMap);
			ShowWindow(hWnd, SW_HIDE);		

			break;
		}

	default:
		break;
	}
	return TRUE;
}


//
// Process mouse mse here
//
BOOL DispatchMouseMsg(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	POINT mousePoint;
	mousePoint.x = LOWORD(lParam);
	mousePoint.y = HIWORD(lParam);

	switch(message)
	{
	case WM_LBUTTONDOWN:
		{	
			if(copyDC.State() == DCUtil::RANG_ENDED
				&& mousePoint.x >= copyDC.Left() 
				&& mousePoint.x <= copyDC.Right()
				&& mousePoint.y <= copyDC.Bottom() 
				&& mousePoint.y >= copyDC.Top())
			{


				pointA.x = LOWORD(lParam);
				pointA.y = HIWORD(lParam);

				_bLeftBtDown = TRUE;
				copyDC.IsOnEdge(pointA, _curPos);
				break;
			}


			// Set point start & paint
			if(copyDC.State() != DCUtil::RANG_ENDED)
			{
				copyDC.CkStart(mousePoint);	
				RePaintBMP(hWnd, _hBitMap, copyDC);	
				break;
			}		

			break;
		}

	case WM_LBUTTONUP:
		{
			_bLeftBtDown = FALSE;
			if(copyDC.State() == DCUtil::RANG_ENDED)
				break;


			copyDC.CkEnd(mousePoint);	
			RePaintBMP(hWnd, _hBitMap, copyDC);	
			break;
		}

	case WM_MOUSEMOVE:
		{   
			// Draw rectangle here
			//
			if(copyDC.State() == DCUtil::RANT_PROCESSING ||
				copyDC.State() == DCUtil::RANG_STARED)
			{
				copyDC.PointTo(mousePoint);
				RePaintBMP(hWnd, _hBitMap, copyDC);	
				break;
			}

			if(copyDC.State() == DCUtil::RANG_ENDED)
				SetCursorIndicate(hWnd, mousePoint, copyDC);



			// Move the drawn area
			//
			if(_bLeftBtDown && copyDC.State() == DCUtil::RANG_ENDED)
			{

				if(_curPos == DCUtil::EDGE_POS_IN)
				{
					copyDC._start.x += mousePoint.x - pointA.x;
					copyDC._start.y += mousePoint.y - pointA.y;
					copyDC._end.x += mousePoint.x - pointA.x;
					copyDC._end.y += mousePoint.y - pointA.y;	


				}
				else if(_curPos >= copyDC.EDGE_POS_LEFT)
				{
					copyDC.ReRangeDC(pointA, mousePoint, _curPos);	
				}
				RePaintBMP(hWnd, _hBitMap, copyDC);		
				pointA = mousePoint;
			}		

			break;
		}

	case WM_LBUTTONDBLCLK:
		{
			RECT rect;
			copyDC.ToRect(rect);
			copyDC.Clr();

			HDC _windowDC = ::GetDC(hWnd);
			HBITMAP rangBitMap = CopyDCToBitmap(_windowDC, &rect);
			::ReleaseDC(hWnd, _windowDC);

			SaveBmpToClipboard(hWnd, rangBitMap);
			ShowWindow(hWnd, SW_HIDE);		
			break;
		}

	case WM_RBUTTONDOWN:
		{			
			if(copyDC.State() == DCUtil::RANG_ENDED && copyDC.IsInRang(mousePoint))
			{				
				POINT cursorPoint;
				::GetCursorPos(&cursorPoint);

				HMENU hMenu;            // top-level menu 
				HMENU hmenuTrackPopup;  // shortcut menu 

				// Load the menu resource.
				hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDC_CONTENTSCREEN));
				if (hMenu == NULL) 
					return FALSE; 

				// TrackPopupMenu cannot display the menu bar so get 
				// a handle to the first shortcut menu. 
				hmenuTrackPopup = ::GetSubMenu(hMenu, 0);
				// Display the shortcut menu. Track the right mouse 
				// button. 

				// Using the first submenu to set the position of the whole menu
				// Windows will adjust the position for you
				//
				TrackPopupMenu(hmenuTrackPopup, 
					TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
					cursorPoint.x, cursorPoint.y, 0, hWnd, NULL); 

				DestroyMenu(hMenu);
			}
			break;
		}

	default:
		break;
	}
	return TRUE;
}